package charts;

import java.awt.Font;
import java.io.File;
import java.util.Enumeration;
import java.util.Hashtable;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

import Utils.ChartUtils;

public class PieChart {
	// 生成饼图数据集对象
	public static PieDataset createDataset(Hashtable<String, Double> key_value) {
		DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
		Enumeration<String> en = key_value.keys();
		while (en.hasMoreElements()) {
			String key = (String) en.nextElement();
			defaultpiedataset.setValue(key, key_value.get(key));
		}
		return defaultpiedataset;
	}

	// 生成图表主对象JFreeChart
	public static void createChart(Hashtable<String, Double> key_value, String title, String path,int width,int height) {
		JFreeChart jfreechart = ChartFactory.createPieChart(title, createDataset(key_value), true, true, false);
		try {
			PiePlot pieplot = (PiePlot) jfreechart.getPlot();
			pieplot.setLabelFont(new Font("SansSerif", Font.BOLD, 12));
			pieplot.setNoDataMessage("No data available");
			pieplot.setCircular(true);
			pieplot.setLabelGap(0.01D);// 间距
			ChartUtils.saveChart(jfreechart,path);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("draw chart finish");
	}

	public static void main(String args[]) {
	}
}
